package com.tarena.lbs.business.dao.mapper;

import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface BusinessMapper  {

    @Select({
            "<script>",
            "SELECT",
            " id as id,",
            " business_name AS businessName,",
            " industry_type AS industryType,",
            " business_head_name AS businessHeadName,",
            " business_head_phone AS businessHeadPhone,",
            " company_address AS companyAddress,",
            " business_license AS businessLicense,",
            " business_introduction AS businessIntroduction,",
            " entry_time AS entryTime,",
            " business_status AS businessStatus,",
            " audit_remarks AS auditRemarks,",
            " business_logo AS businessLogo",
            " FROM lbs_business",
            " <where>",
            "  <if test=\"q.businessName != null and q.businessName != ''\">",
            "    AND business_name LIKE CONCAT('%', #{q.businessName}, '%')",
            "  </if>",
            "  <if test=\"q.businessHeadPhone != null and q.businessHeadPhone != ''\">",
            "    AND business_head_phone = #{q.businessHeadPhone}",
            "  </if>",
            "  <if test=\"q.businessStatus != null\">",
            "    AND business_status = #{q.businessStatus}",
            "  </if>",
            "  <if test=\"q.startingTime != null\">",
            "    AND entry_time &gt;= #{q.startingTime}",
            "  </if>",
            "  <if test=\"q.endTime != null\">",
            "    AND entry_time &lt;= #{q.endTime}",
            "  </if>",
            " </where>",
            " ORDER BY id DESC",
            " LIMIT #{offset}, #{pageSize}",
            "</script>"
    })
    List<BusinessVO> selectPage(@Param("q") BusinessQuery query,
                                @Param("offset") int offset,
                                @Param("pageSize") int pageSize);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM lbs_business",
            " <where>",
            "  <if test=\"q.businessName != null and q.businessName != ''\">",
            "    AND business_name LIKE CONCAT('%', #{q.businessName}, '%')",
            "  </if>",
            "  <if test=\"q.businessHeadPhone != null and q.businessHeadPhone != ''\">",
            "    AND business_head_phone = #{q.businessHeadPhone}",
            "  </if>",
            "  <if test=\"q.businessStatus != null\">",
            "    AND business_status = #{q.businessStatus}",
            "  </if>",
            "  <if test=\"q.startingTime != null\">",
            "    AND entry_time &gt;= #{q.startingTime}",
            "  </if>",
            "  <if test=\"q.endTime != null\">",
            "    AND entry_time &lt;= #{q.endTime}",
            "  </if>",
            " </where>",
            "</script>"
    })
    Long count(@Param("q") BusinessQuery query);

    @Select("SELECT id, business_name AS businessName, industry_type AS industryType, business_head_name AS businessHeadName, business_head_phone AS businessHeadPhone, company_address AS companyAddress, business_license AS businessLicense, business_introduction AS businessIntroduction, entry_time AS entryTime, business_status AS businessStatus, audit_remarks AS auditRemarks, business_logo AS businessLogo FROM lbs_business WHERE id = #{id}")
    BusinessPO selectById(@Param("id") Integer id);

    @Insert({
            "INSERT INTO lbs_business(",
            " business_name, industry_type, business_head_name, business_head_phone,", 
            " company_address, business_license, business_introduction, entry_time,", 
            " business_status, audit_remarks, business_logo)",
            " VALUES(",
            " #{businessName}, #{industryType}, #{businessHeadName}, #{businessHeadPhone},",
            " #{companyAddress}, #{businessLicense}, #{businessIntroduction}, #{entryTime},",
            " #{businessStatus}, #{auditRemarks}, #{businessLogo}",
            ")"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BusinessPO po);

    @Update({
            "<script>",
            "UPDATE lbs_business",
            " <set>",
            "  <if test='businessName != null'> business_name = #{businessName}, </if>",
            "  <if test='industryType != null'> industry_type = #{industryType}, </if>",
            "  <if test='businessHeadName != null'> business_head_name = #{businessHeadName}, </if>",
            "  <if test='businessHeadPhone != null'> business_head_phone = #{businessHeadPhone}, </if>",
            "  <if test='companyAddress != null'> company_address = #{companyAddress}, </if>",
            "  <if test='businessLicense != null'> business_license = #{businessLicense}, </if>",
            "  <if test='businessIntroduction != null'> business_introduction = #{businessIntroduction}, </if>",
            "  <if test='entryTime != null'> entry_time = #{entryTime}, </if>",
            "  <if test='businessStatus != null'> business_status = #{businessStatus}, </if>",
            "  <if test='auditRemarks != null'> audit_remarks = #{auditRemarks}, </if>",
            "  <if test='businessLogo != null'> business_logo = #{businessLogo} </if>",
            " </set>",
            " WHERE id = #{id}",
            "</script>"
    })
    int update(BusinessPO po);

    @Delete("DELETE FROM lbs_business WHERE id = #{id}")
    int deleteById(@Param("id") Integer id);
}
